CountryNames = [CountryNames;'Developed ex-U.S.   ';'Emerging            ';'G7 ex-U.S.          ';'Europe              ';'Asia-Pacific        ';'All ex-U.S.         '];
NumAllcsum = [0; cumsum(NumAll)];
CountryRegionDummy = false(N,55); CountryRegionDummy(:,55) = true;

for cntry = 1:49
    idx = NumAllcsum(cntry)+1:NumAllcsum(cntry+1);
    CountryRegionDummy(idx,cntry) = true;
end
clear cntry idx;

CntryIndx = []; i1 = length(Developed);
for i2 = 1:i1; i3 = Developed(i2); CntryIndx = [CntryIndx NumAllcsum(i3)+1:NumAllcsum(i3+1)]; end
CountryRegionDummy(CntryIndx,50) = true;

CntryIndx = []; i1 = length(Emerging);
for i2 = 1:i1; i3 = Emerging(i2); CntryIndx = [CntryIndx NumAllcsum(i3)+1:NumAllcsum(i3+1)]; end
CountryRegionDummy(CntryIndx,51) = true;

CntryIndx = []; i1 = length(G7);
for i2 = 1:i1; i3 = G7(i2); CntryIndx = [CntryIndx NumAllcsum(i3)+1:NumAllcsum(i3+1)]; end
CountryRegionDummy(CntryIndx,52) = true;

CntryIndx = []; i1 = length(Europe);
for i2 = 1:i1; i3 = Europe(i2); CntryIndx = [CntryIndx NumAllcsum(i3)+1:NumAllcsum(i3+1)]; end
CountryRegionDummy(CntryIndx,53) = true;

CntryIndx = []; i1 = length(AsiaPacific);
for i2 = 1:i1; i3 = AsiaPacific(i2); CntryIndx = [CntryIndx NumAllcsum(i3)+1:NumAllcsum(i3+1)]; end
CountryRegionDummy(CntryIndx,54) = true;
clear i1 i2 i3 CntryIndx;

clear AsiaPacific CountryCodes CountryNames Developed Emerging Europe G7 NumAll NumAllcsum33;

% set zero mcap to missing
MV(MV==0) = NaN; MV(~isfinite(MV)) = NaN;  

% figure out REITs
IsREIT = false(N,1); 
if ExcludeREIT
    load REIT;
    [~,tmp,~] = intersect((1:N)',data);
    IsREIT(tmp) = true;
    clear data tmp;
end

% make micro
IsMicro = false(N,T,55);
for cntry = 1:55
    for t = 6:12:T
        % find mcap breakpoint
        idx = CountryRegionDummy(:,cntry) & ~IsREIT;
        m = MV(idx,t); m(~isfinite(m)) = []; m = sort(m);
        if length(m)<10
            continue;
        end
        m(end-4:end) = m(end-4);
        mcsum = cumsum(m);
        ii = mcsum <= 0.03*mcsum(end); 
        fii = find(ii);
        brkpt = m(fii(end));
        idx = CountryRegionDummy(:,cntry) & MV(:,t) <= brkpt;
        for s = t:min(t+11,T)
            IsMicro(idx,s,cntry) = true;
        end
    end
end
clear cntry t m mcsum mtotal idx s ii fii brkpt;
